package interfaze;

import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import logic.GlobalMenuThread;
import logic.Mp3Player;
import logic.Utilities;

/** The Global Menu Panel's class.
 * @author 
 *
 */
@SuppressWarnings("serial")
public class GlobalMenuPanel extends JPanel implements ActionListener, MouseListener {
	
	private SuperArcade2in1 window;									//The JFrame in which this panel is being added.
	private JButton bPlay = new JButton("Play");					//The play button.
	private JButton bGlobalBoard = new JButton("Global Board");		//The global board button.
	private JButton bSignOut = new JButton("Sign out"); 			//The sign out button.
	private JButton bExit = new JButton("Exit");					//The exit button.
	private JLabel lTitle = new JLabel();							//The title label.
	private JLabel background1 = new JLabel();						//One part of the background.
	private JLabel background2 = new JLabel();						//The other part of the background.
	private ImageIcon titleOnIcon;									//The title's on imageIcon.
	private ImageIcon titleOffIcon;									//The title's off imageIcon.
	private boolean isTitleOn = true;								//A flag to know if the title is on or off.
	private Font font;							//The font that is going to be used for all the buttons of the panel.
	private GlobalMenuThread thread;			//The thread that gives the lightning effect to the title.
	
	/**GlobalMenuPanel's constructor.
	 * @param window : The JFrame in which this panel is being added.
	 */
	public GlobalMenuPanel(SuperArcade2in1 window) {
		this.window = window;
		
		//Sets panel's layout to null in order to work with absolute coordinates.
		this.setLayout(null);
		
		//If the music is stopped.
		if(window.player == null) {
			//Starts new music.
			window.player = new Mp3Player("res/Lastation.mp3");
			window.player.start();
		}
		
		//Sets the font that is going to be used by all the buttons.
		font = new Font("Helvetica", Font.PLAIN, Math.round(12 * window.xScale));
		
		//Sets the components of the panel, their boundaries, icons, fonts, and so on.
		Image image = Utilities.loadImage("res/BrickBreakerOn.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/BrickBreakerOn.png");
		}
		background1.setIcon(new ImageIcon(image));
		background1.setBounds(0, 0, image.getWidth(null), image.getHeight(null));
		
		image = Utilities.loadImage("res/EarthDefenderOn.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/EarthDefenderOn.png");
		}
		
		background2.setIcon(new ImageIcon(image));
		background2.setBounds(0, 0, image.getWidth(null), image.getHeight(null));
		
		image = Utilities.loadImage("res/SuperArcade2in1on.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/SuperArcade2in1on.png");
		}
		titleOnIcon = new ImageIcon(image);
		image = Utilities.loadImage("res/SuperArcade2in1off.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/SuperArcade2in1off.png");
		}
		titleOffIcon = new ImageIcon(image);
		lTitle.setIcon(titleOnIcon);
		lTitle.setBounds(window.width/2 - image.getWidth(null)/2, window.height/2 - image.getHeight(null),
				image.getWidth(null), image.getHeight(null));
		
		bPlay.setFont(font);
		bPlay.setBounds(window.width / 2 - Math.round(120 * window.xScale) / 2,
				Math.round(350 * window.yScale), Math.round(120 * window.xScale), Math.round(25 * window.yScale));
		
		bGlobalBoard.setFont(font);
		bGlobalBoard.setBounds(window.width / 2 - Math.round(120 * window.xScale) / 2,
				Math.round(380 * window.yScale), Math.round(120 * window.xScale), Math.round(25 * window.yScale));
		
		bSignOut.setFont(font);
		bSignOut.setBounds(window.width / 2 - Math.round(120 * window.xScale) / 2,
				Math.round(410 * window.yScale), Math.round(120 * window.xScale), Math.round(25 * window.yScale));
		
		bExit.setFont(font);
		bExit.setBounds(window.width / 2 - Math.round(120 * window.xScale) / 2,
				Math.round(440 * window.yScale), Math.round(120 * window.xScale), Math.round(25 * window.yScale));
		
		//Add the components to the panel.
		this.add(lTitle);
		this.add(bPlay);
		this.add(bGlobalBoard);
		this.add(bSignOut);
		this.add(bExit);
		this.add(background2);
		this.add(background1);
		
		//Add the listener to the components.
		bPlay.addActionListener(this);
		bPlay.addMouseListener(this);
		bGlobalBoard.addActionListener(this);
		bGlobalBoard.addMouseListener(this);
		bSignOut.addActionListener(this);
		bSignOut.addMouseListener(this);
		bExit.addMouseListener(this);
		bExit.addActionListener(this);
		
		//Sets focusable and reques te focus.
		this.setFocusable(true);
		this.requestFocus();
		
		//Starts the effect of the title.
		thread = new GlobalMenuThread(this);
		thread.start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//Stops the title lightning effect.
		thread.interrupt();
		if(e.getSource().equals(bPlay)) {
			//Redirects to the game selector panel.
			window.add(new GameSelectorPanel(window));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bGlobalBoard)) {
			//Redirects to global leader board.
			window.add(new GlobalLeaderBoardPanel(window));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bSignOut)) {
			//Dispose this JFrame and redirects to login window.
			window.player.stopPlayer();
			window.player = null;
			if(window.isOnFullScreen) {
				window.isOnFullScreen = false;
				Utilities.windowToFullScreen(window);
			}
			window.dispose();
			window = null;
			new Login();
		} else if(e.getSource().equals(bExit)) {
			//Exits from the game.
			System.exit(0);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		//Increases the buttons size when the mouse enters them.
		if(e.getSource().equals(bPlay)) {
			Utilities.buttonEffect(bPlay, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bGlobalBoard)) {
			Utilities.buttonEffect(bGlobalBoard, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bSignOut)) {
			Utilities.buttonEffect(bSignOut, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bExit)) {
			Utilities.buttonEffect(bExit, 5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		//Decreases the buttons size when the mouse enters them.
		if(e.getSource().equals(bPlay)) {
			Utilities.buttonEffect(bPlay, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bGlobalBoard)) {
			Utilities.buttonEffect(bGlobalBoard, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bSignOut)){
			Utilities.buttonEffect(bSignOut, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bExit)) {
			Utilities.buttonEffect(bExit, -5, window.xScale, window.yScale);
		}
	}

	/**Changes the image of the title if it's on it set to off and the other way around.
	 */
	public void changeTitle() {
		if (isTitleOn) {
			lTitle.setIcon(titleOffIcon);
			isTitleOn = false;
		} else {
			lTitle.setIcon(titleOnIcon);
			isTitleOn = true;
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

}
